//
//  CZTabBarController.m
//  i0S007通用项目
//
//  Created by Apple on 16/2/17.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "CZTabBarController.h"
#import "CZHomeViewController.h"
#import "CZAboutMeViewController.h"
#import "CZZoneTableViewController.h"
#import "CZMoreViewController.h"
#import "CZMoodViewController.h"

// 加号按钮宽度增加的尺寸
const int AddButtonWidthOffest = 4;

@interface CZTabBarController ()

@property (nonatomic, strong) UIButton *addButton;

@end

@implementation CZTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 动态
    CZHomeViewController *homeVC = [[CZHomeViewController alloc] init];
    [self addchildViewCOntroller:homeVC title:@"动态" imageName:@"tabbar_icon_auth"];
    
    // 与我相关
    CZAboutMeViewController *aboutMeVC = [[CZAboutMeViewController alloc] init];
    [self addchildViewCOntroller:aboutMeVC title:@"与我相关" imageName:@"tabbar_icon_at"];
    
    // 添加一个占位的控制器,目的是让tabBar空出一个位置来方加号按钮
    [self addChildViewController:[[UIViewController alloc] init]];
    
    // 我的空间
    CZZoneTableViewController *zoneVC = [[CZZoneTableViewController alloc] init];
    [self addchildViewCOntroller:zoneVC title:@"我的空间" imageName:@"tabbar_icon_space"];
    
    // 更多
    CZMoreViewController *moreVC = [[CZMoreViewController alloc] init];
    [self addchildViewCOntroller:moreVC title:@"更多" imageName:@"tabbar_icon_more"];

    // 在这里调用这个方法,把按钮添加到tabBar时机太早,会被系统后来添加的item给盖住了
//    [self setupAddButton];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
//    [self setupAddButton];
}

// 控制器的view将要开始布局,只要控制器的尺寸发生改变
- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    [self setupAddButton];
}

// 设置加号按钮
- (void)setupAddButton {
    // 1.将按钮添加为tabBar的子控件
    [self.tabBar addSubview:self.addButton];
    
    // 2.计算按钮的宽度
    CGFloat width = self.tabBar.frame.size.width / 5;
    
    // 3.设置frame
    self.addButton.frame = CGRectMake(2 * width - AddButtonWidthOffest / 2, 0, width + AddButtonWidthOffest, self.tabBar.frame.size.height);
}

/**
 *  添加tabBar的子控件
 *
 *  @param vc        控制器
 *  @param title     标题
 *  @param imageName 普通的图片名称
 */
- (void)addchildViewCOntroller:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName {
    // 设置标题
    vc.title = title;
    
    // 设置普通图片
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    
    // 拼接高亮图片名称
    NSString *highlightedImageName = [NSString stringWithFormat:@"%@_click", imageName];
    
    // 设置高亮图片,图片不要被渲染
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:highlightedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 修改高亮时的文字颜色
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]} forState:UIControlStateSelected];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    
    [self addChildViewController:nav];
}

#pragma mark 点击事件
- (void)addButtonClick {
    CZMoodViewController *mood = [[CZMoodViewController alloc] init];
    mood.title = @"写说说";
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mood];
    
    // UIModalPresentationFormSheet 只在iPad上面有效果
    nav.modalPresentationStyle = UIModalPresentationFormSheet;
    
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark - 懒加载
- (UIButton *)addButton {
    if (_addButton == nil) {
        _addButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        // 设置图片
        [_addButton setImage:[UIImage imageNamed:@"tabbar_btn"] forState:UIControlStateNormal];
        
        // 设置图片的填充模式
        _addButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
        
        // 添加点击事件
        [_addButton addTarget:self action:@selector(addButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _addButton;
}

@end
